//
//  WYPublic.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/18.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "WYPublic.h"
#import "AppDelegate.h"
#import "DBBLoginVC.h"
#import "DBBRegistVC.h"
#import "PublicActivityView.h"
#import "PopView.h"

#import "WYMainTabBarVC.h"
#import "MyDuoBaoVC.h"

#define CZ_TONGJI_URL @"charge"
@implementation WYPublic
//做适配创建frame的方法
+ (CGRect)getRectWithX:(CGFloat)x :(CGFloat) y :(CGFloat)width :(CGFloat)height
{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    CGRect rect;
    rect.origin.x = x * myDelegate.autoSizeScaleX;
    rect.origin.y = y * myDelegate.autoSizeScaleY;
    rect.size.width = width * myDelegate.autoSizeScaleX;
    rect.size.height = height * myDelegate.autoSizeScaleY;
    return rect;
}
//判断用户是否登录，没有则跳转到登陆界面
+ (BOOL)ifUserHaveLogin
{
    if ([UserInfo isUserLogin] == NO){
        
        DBBLoginVC *loginVC = [DBBLoginVC shareVC];
        UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:loginVC];
        navController.navigationBarHidden = YES;
        
        UIViewController *selectedVC = [WYMainTabBarVC shareMainTaBarVC].selectedViewController;
        [selectedVC.navigationController presentViewController:navController animated:YES completion:nil];
        
        return NO;
    }
    return YES;
    
}
//
//+ (void)showMessageWithTitle:(NSString *)title Msg:(NSString *)message view:(UIView*)view completion:(void(^)(void))completion{
//    
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    [view addSubview:hud];
//    hud.mode = MBProgressHUDModeText;
//    hud.labelText = message;
////    [hud showAnimated:YES whileExecutingBlock:nil completionBlock:completion];
//    hud.completionBlock = completion;
//    [hud hide:YES afterDelay:1.5f];
//    
//}
//用户提示信息
+ (void)showMessageWithTitle:(NSString *)title Msg:(NSString *)message
{
    
//    PopView *popView = [[PopView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 40)];
//    popView.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2+100);
//    [WINDOW addSubview:popView];
//    [popView setText:message];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:HUD_ALPHA]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showInfoWithStatus:message];
}

//用户提示错误信息
+ (void)showErrorWithTitle:(NSString *)title Msg:(NSString *)message{
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:HUD_ALPHA]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showErrorWithStatus:message];
    
    
    
}

//给window添加加载提示hud
+ (void)addHudToWindow
{
    //    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    //    for (UIView *v in window.subviews) {
    //        if (v.tag == 2222) {
    //            [v removeFromSuperview];
    //        }
    //    }
    ////    [[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:NO];
    //    PublicActivityView *tumblrHUD = [[PublicActivityView alloc] initWithFrame:CGRectMake(0,0,60,60)];
    //    tumblrHUD.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
    //    tumblrHUD.tag = 2222;
    //    [WINDOW addSubview:tumblrHUD];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:HUD_ALPHA]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    
    [SVProgressHUD showWithStatus:@"正在加载中.."];
}
//一个加载 显示自定义的 文字
+ (void)addHudToWindowWithTitle:(NSString *)title
{
    //    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    //    for (UIView *v in window.subviews) {
    //        if (v.tag == 2222) {
    //            [v removeFromSuperview];
    //        }
    //    }
    ////    [[[UIApplication sharedApplication] keyWindow] setUserInteractionEnabled:NO];
    //    PublicActivityView *tumblrHUD = [[PublicActivityView alloc] initWithFrame:CGRectMake(0,0,60,60)];
    //    tumblrHUD.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
    //    tumblrHUD.tag = 2222;
    //    [WINDOW addSubview:tumblrHUD];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:HUD_ALPHA]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setDefaultMaskType: SVProgressHUDMaskTypeClear];
    [SVProgressHUD showWithStatus:title];
}

//去掉加载提示
+ (void)removeHudToWindow
{
    
    //    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    //    UIView *zhezhao = [window viewWithTag:2222];
    //    [zhezhao removeFromSuperview];
    
    //    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:HUD_ALPHA]];
    [SVProgressHUD dismissWithDelay:HUD_DELAY_TIME];
    
}

/**
 *判断是否是邮箱
 **************/
+ (BOOL)iseMailAddress:(NSString *)candidate
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    if ([emailTest evaluateWithObject:candidate]) {
        [WYPublic showMessageWithTitle:nil Msg:@"您输入的邮箱格式有误！"];
        return NO;
    }
    return YES;
}
/**
 *判断手机号是否合法
 **************/
+ (BOOL)isPhoneNum:(NSString *)candidate
{
    //匹配手机号码
    NSString *emailRegex = @"1[3|4|5|7|8][0-9]\\d{4,8}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    if (candidate.length < 11) {
        [WYPublic showMessageWithTitle:nil Msg:@"手机号码位数不足！"];
        return NO;
    }
    if (![emailTest evaluateWithObject:candidate]) {
        [WYPublic showMessageWithTitle:nil Msg:@"您输入的手机号码格式有误！"];
        return NO;
    }
    return YES;
}

//给window添加一个遮罩层
+ (void)addZheZhaoToWindowWithTitle:(NSString *)title
{
    [WYPublic removeZheZhaoToWindow];
    UIView *zheZhao = [[UIView alloc]initWithFrame:CGRectMake(0,0,WYMWITH, WYMHEIGHT)];
    zheZhao.tag = 1111;
    zheZhao.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    ac.center = CGPointMake(WYMWITH/2, WYMHEIGHT/2);
    [ac startAnimating];
    [zheZhao addSubview:ac];
    
    if (title != nil) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 22)];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.center = CGPointMake(WYMWITH/2, CGRectGetMaxY(ac.frame));
        lab.text = title;
        lab.textColor = [UIColor whiteColor];
        lab.font = [UIFont systemFontOfSize:15];
        [zheZhao addSubview:lab];
    }
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:zheZhao];
}
//去掉window的遮罩
+ (void)removeZheZhaoToWindow
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    UIView *zhezhao = [window viewWithTag:1111];
    [zhezhao removeFromSuperview];
}
//返回到登录注册以前的界面
+ (void)logOrRegistSuccess
{
//    AppDelegate *app = [UIApplication sharedApplication].delegate;
//    app.window.rootViewController = app.rtVC;
    DBBLoginVC *vc = [DBBLoginVC shareVC];
    [vc dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"logOrRegistSuccess");
}

//获取一条横着的分割线
+ (UIView *)getFenGexian:(CGFloat)x :(CGFloat)y :(CGFloat)width :(CGFloat)height
{
    UIView *fenge = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    return fenge;
}

+ (UIView *)getLineView:(CGFloat)x orginY:(CGFloat)y sizeWidth:(CGFloat)width sizeHeight:(CGFloat)height
{
    UIView *fenge = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    return fenge;
}
//获取中间红两边灰的字符串
+ (NSMutableAttributedString *)getRedMiddleStrWithLeft:(NSString *)leftStr Middle:(NSString*)middleStr RightStr:(NSString*)rightStr Font:(CGFloat)font
{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@%@",leftStr,middleStr,rightStr]];
    [str addAttribute:NSForegroundColorAttributeName value:QBB_RED_COLOR range:NSMakeRange(leftStr.length,middleStr.length)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(0, str.length)];
    return str;
}
//获取当前控制器
+(UIViewController *)GetCurrentviewController{
    /// Finds the view's view controller.
    // Traverse responder chain. Return first found view controller, which will be the view's view controller.
    UINavigationController *navi = [WYMainTabBarVC shareMainTaBarVC].navigationController;
    if (navi.viewControllers.count > 0) {
        return (UIViewController *)navi.viewControllers[navi.viewControllers.count - 1];
    }
    return nil;
}
+(NSString *)getUsefullStr:(id)str
{
    if ([str class] != [NSNull class] && str != nil) {
        return str;
    }
    return @"";
}


/**************
 *充值成功统计
 *****************/
- (void)tongji:(NSString *)money
{
    NSDictionary *params = @{@"money":money};
    [WYRequest startPostForTongJiWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_YX_IP,CZ_TONGJI_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        
    } failedBlock:^{
        
    } needLog:NO];
}

#pragma mark -- 从storybaord上出事化一个controller

+ (UIViewController *)controllerWithStoryBoardName:(NSString*)storyBoardName identifier:(NSString*)identifier{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
    
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:identifier];
    return vc;
}
@end
